package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.consts.ErmsCodePrefix;
import com.sugon.dao.ModuleSpDao;
import com.sugon.entity.ModuleSpEntity;
import com.sugon.service.ModuleSpService;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.StringUtils;
import com.sugon.utils.excel.ExcelImport;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 模具备件清单Service实现类
 *
 * @author YuXD
 * @date 2021-12-03 16:21:04
 */
@Service("moduleSpService")
public class ModuleSpServiceImpl implements ModuleSpService {

    @Resource
    private ModuleSpDao moduleSpDao;

    /**
     *
     * @param id 主键
     * @return
     */
    @Override
    public ModuleSpEntity queryObject(String id) {
        return moduleSpDao.queryObject(id);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<ModuleSpEntity> queryList(Map<String, Object> map) {
        return moduleSpDao.queryList(map);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleSpDao.queryTotal(map);
    }

    /**
     *
     * @param uniqueBarCode
     * @return
     */
    @Override
    public ModuleSpEntity queryByUniqueBarCode(String uniqueBarCode) {
        Map<String, Object> queryParam = MapUtil.newHashMap();
        queryParam.put("uniqueBarCode", uniqueBarCode);
        List<ModuleSpEntity> moduleSpEntities = queryList(queryParam);
        return CollectionUtil.isNotEmpty(moduleSpEntities) ? moduleSpEntities.get(0) : null;
    }

    /**
     *
     * @param moduleSp 实体
     * @return
     */
    @Override
    public int save(ModuleSpEntity moduleSp) {
        moduleSp.setId(RandomUtil.randomString(32));
        moduleSp.setPartCode(generateUniqueCode());
        moduleSp.setGmtCreate(DateUtil.date());
        moduleSp.setCreateUser(ShiroUtils.getUserId());
        return moduleSpDao.save(moduleSp);
    }

    /**
     *
     * @param moduleSp 实体
     * @return
     */
    @Override
    public int update(ModuleSpEntity moduleSp) {
        moduleSp.setGmtModified(DateUtil.date());
        moduleSp.setModifyUser(ShiroUtils.getUserId());
        return moduleSpDao.update(moduleSp);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return moduleSpDao.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return moduleSpDao.deleteBatch(ids);
    }

    /**
     * 生成模具型芯唯一码
     * @param object
     * @return
     */
    @Override
    public synchronized String generateUniqueCode(Object object) {
        String spareUniqueCode = (String) object;
        if (StrUtil.isEmpty(spareUniqueCode)) {
            // 获取DB中最新记录
            spareUniqueCode = moduleSpDao.getLastestPartCode();
        }
        // 生成新的唯一编码
        spareUniqueCode = com.sugon.modules.utils.StrUtil.increase(spareUniqueCode, ErmsCodePrefix.MOLD_SPARE_PART_CODE_PREFIX, 6, 1);
        // 确保最新生成备件编码不重复
        ModuleSpEntity sparePart = moduleSpDao.queryByCode(spareUniqueCode);
        if (sparePart == null) {
            return spareUniqueCode;
        }
        return generateUniqueCode(spareUniqueCode);
    }

    /**
     * 批量导入
     * @param file
     * @return
     */
    @Override
    public int batchImport(MultipartFile file) {
        //获取所有表格数据
        List<String[]> list = ExcelImport.getExcelData(file);
        int count = 0;
        for (int i = 1; i < list.size(); i++) {
            ModuleSpEntity moduleSp = null;
            boolean isSave;
            String[] array1 = list.get(i);
            String[] array = new String[7];
            System.arraycopy(array1, 0, array, 0, array1.length);
            // 根据选项编码是否填写确定逻辑
            /*
                如果系统编码不为空，则根据系统编码查询，否则根据名称+图号查询
             */
            String partCode = array[6];
            String name = array[1];
            String mapNo = array[2];
            if (StrUtil.isNotEmpty(partCode)) {
                moduleSp = moduleSpDao.queryByCode(partCode);
            } else {
                List<ModuleSpEntity> moduleSpList = moduleSpDao.queryByNameAndMapNo(name, mapNo);
                if (CollectionUtil.isNotEmpty(moduleSpList)) {
                    moduleSp = moduleSpList.get(0);
                }
            }
            isSave = moduleSp == null;
            if (moduleSp == null) {
                moduleSp = new ModuleSpEntity();
            }
            moduleSp.setName(name);
            moduleSp.setMapNo(mapNo);
            // 型芯类别
            if (StringUtils.isNotEmpty(array[0])) {
                moduleSp.setType(array[0]);
            }
            //  条码代码
            if (StringUtils.isNotEmpty(array[3])) {
                moduleSp.setUniqueBarCode(array[3]);
            }
            //单位
            if (StringUtils.isNotEmpty(array[4])) {
                moduleSp.setUnit(array[4]);
            }
            //维护标准
            if (StringUtils.isNotEmpty(array[5])) {
                moduleSp.setRemark(array[5]);
            }
            if (isSave) {
                count += save(moduleSp);
            } else {
                count += update(moduleSp);
            }
        }
        return count;
    }
}
